package com.server.netty;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.sandking.tools.SK_ContentTypeUnit;
import com.sandking.tools.SK_Map;
import com.sandking.tools.SK_String;

public class HttpLogicDisp {
	//HTTP 逻辑分发
	public static Map<String, Object> disp(URI uri, Map<String, Object> param){
		Map<String, Object> ret = new HashMap<String, Object>();
		String contentType = null;
		byte[] content = null;
		String path = uri.getPath();
		System.out.println("ptah = " + path + " param = " +param);
		if (path.startsWith("/sk")) {
			contentType = "text/html";
			String c =param.get("c").toString();
			String m =param.get("m").toString();
			Map<String, Object> d = SK_Map.getMap("d", param);
			if (SK_String.isEmpty(c)||SK_String.isEmpty(m)) {
//				return 
			}
			
			if (c.equals("LoginI")) {
				if (m.equals("login")) {
					Map map = new HashMap<>();
					map.put("a", "b");
					String json = JSON.toJSONString(map);
					content = json.getBytes();
				}
			}
		}else {
			path = path.substring(1);
			Path p = Paths.get(path);
			try {
				contentType = Files.probeContentType(p);
				if (contentType==null) {
					contentType = SK_ContentTypeUnit.getType(path.substring(path.lastIndexOf(".")));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				content = FileUtils.readFileToByteArray(new File(path));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		ret.put("content", content);
		ret.put("contentType", contentType);
		return ret;
	}
	
	public static void main(String[] args) {
		
	}
}
